package com.javasm.controller.MisMannageController;

import com.alibaba.fastjson.JSON;
import com.javasm.entity.MisMannageEntity.BuyFund;
import com.javasm.entity.MisMannageEntity.PageInfo;
import com.javasm.entity.MisMannageEntity.ReturnCode1;
import com.javasm.entity.MisMannageEntity.ReturnEntity;
import com.javasm.service.MisMannageService.BuyFundService;
import com.javasm.service.MisMannageService.TypeService;
import com.javasm.service.MisMannageService.impl.BuyFundServiceImpl;
import com.javasm.service.MisMannageService.impl.TypeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author:JAVASM
 * @description:
 * @date:2022/5/26 10:39
 * @version:0.1
 * @since:jdk11
 */
@WebServlet("/buyFund")
public class BuyFundServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        ReturnEntity re = new ReturnEntity();
        String productType = req.getParameter("productType");

        if (productType!=null && !"".equals(productType)){//1.先根据产品类型名称查产品类型id
            TypeService ts = new TypeServiceImpl();
            //查到产品类型编号
            Integer typeId = ts.getTypeId(productType);
            //2.接收page和pageSize
            String pageStr = req.getParameter("page");
            String pageSizeStr = req.getParameter("pageSize");
            Integer page = 1;
            Integer pageSize = 10;
            if (pageStr!=null && !"".equals(pageStr)){
                page = Integer.parseInt(pageStr);
            }
            if (pageSizeStr!=null && !"".equals(pageSizeStr)){
                pageSize = Integer.parseInt(pageSizeStr);
            }

            BuyFundService bfs = new BuyFundServiceImpl();
            //传入page,pageSize,typeId 是否开启线上申购在service中判断
            List<BuyFund> fundByPage = bfs.getFundByPage(page, pageSize, typeId);

            //获取当前每页记录数的总页数
            Integer fundTotalPage = bfs.getFundTotalPage(pageSize);

            Integer fundTotalNum = bfs.getFundTotalNum();
            if (fundByPage.size()>0){
                re.setRetCode(ReturnCode1.OPERATE_OK.getCode());
                re.setRetMsg(ReturnCode1.OPERATE_OK.getMsg());
                if (page>=fundTotalPage){
                    re.setNextPageStatus(false);
                }else {
                    re.setNextPageStatus(true);
                }

                re.setRetData(fundByPage);
                re.setPageInfo(new PageInfo(page, pageSize, fundTotalNum));
            }else {
                re.setRetCode(ReturnCode1.OPERATE_FAILED.getCode());
                re.setRetMsg(ReturnCode1.OPERATE_FAILED.getMsg());
                re.setPageInfo(new PageInfo(1,0,0));
            }

            resp.setContentType("application/json;charset=utf-8");
            PrintWriter writer = resp.getWriter();
            writer.print(JSON.toJSONString(re));
            writer.flush();
            writer.close();

        }else {//没有产品类型查不到
            re.setRetCode(ReturnCode1.OPERATE_FAILED.getCode());
            re.setRetMsg(ReturnCode1.OPERATE_FAILED.getMsg());

            resp.setContentType("application/json;charset=utf-8");
            PrintWriter writer = resp.getWriter();
            writer.print(JSON.toJSONString(re));
            writer.flush();
            writer.close();
        }

    }
}
